/*-------------------------------------------------------------------------
   timer.h
   Copyright (C) 2010 One Laptop per Child

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

   In other words, you are welcome to use, share and improve this program.
   You are forbidden to forbid anyone else to use, share and improve
   what you give them.   Help stamp out software-hoarding!

   As a special exception, you may use this file as part of a free software
   library for the XO of the One Laptop per Child project without restriction.
   Specifically, if other files instantiate
   templates or use macros or inline functions from this file, or you compile
   this file and link it with other files to produce an executable, this
   file does not by itself cause the resulting executable to be covered by
   the GNU General Public License.  This exception does not however
   invalidate any other reasons why the executable file might be covered by
   the GNU General Public License.
-------------------------------------------------------------------------*/
#ifndef TIMER_H
#define TIMER_H


void watchdog_start(unsigned int);
void watchdog_stop(void);
void watchdog_reboot(void);

#ifdef UNUSED
// unused
void intsrv_timer0(void) EXTERN_INTERRUPT(1);

// unused
void intsrv_gptimer0(void) EXTERN_INTERRUPT(20);
void intsrv_gptimer1(void) EXTERN_INTERRUPT(21);
void intsrv_gptimer2(void) EXTERN_INTERRUPT(22);
#endif

void intsrv_gptimer3(void) EXTERN_INTERRUPT(23);

void timer_gpt3_init(void);
void short_delay(unsigned int n);  // try not to call this.  use usec_delay()
int get_ms_timer(void);
unsigned char check_timer(int time0, int duration);
void ms_delay_block(int duration);
void print_tstamp(void);
long get_ms_timer_ext(void);
unsigned char check_timer_ext(long time0, long duration);

#ifdef SDCC
#define usecs_per_10_loops 31   // 3.1 usec per loop
#else // Keil
#define usecs_per_10_loops 17   // 1.7 usec per loop
#endif
#define usecs_to_loops(u) (10*(long)(u)/usecs_per_10_loops)

#define usec_delay(usecs) short_delay(usecs_to_loops(usecs)+1)

#endif